package com.cz.nongyetongv5.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.bean.JianChaDanWei;
import com.cz.nongyetongv5.bean.JianChaRenYuan;
import com.cz.nongyetongv5.bean.TextInfo;

public class JianChaDanWeiListAdapter extends BaseAdapter {

	// 上下文对象
	private Context context = null;

	private List<TextInfo> datas = null;

	// 所有单选按钮的集合
	private List<CheckBox> checkBoxs = null;

	public JianChaDanWeiListAdapter(Context context, List<TextInfo> datas,
									List<CheckBox> checkBoxs) {
		this.context = context;
		this.datas = datas;
		this.checkBoxs = checkBoxs;
	}

	@Override
	public int getCount() {
		return datas == null ? 0 : datas.size();
	}

	@Override
	public Object getItem(int arg0) {
		return datas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {

		ViewGroup jianChaDanWeiLayout = null;

		if (view == null) {
			jianChaDanWeiLayout = (ViewGroup) LayoutInflater.from(context)
					.inflate(R.layout.jianchadanwei_item_layout, null);
		} else {
			jianChaDanWeiLayout = (ViewGroup) view;
		}

		String lineData = datas.get(position).mText;

		// 设置每一行的文本
		TextView jianChaDanWeiTextView = (TextView) jianChaDanWeiLayout
				.findViewById(R.id.tvJianChaDanWeiItem);
		jianChaDanWeiTextView.setText(lineData);

		// 检查单位的选择
		CheckBox jianChaDanWeiCheckbox = (CheckBox) jianChaDanWeiLayout
				.findViewById(R.id.cbJianChaDanWeiItem);

		jianChaDanWeiCheckbox.setChecked(false);

		if (!checkBoxs.contains(jianChaDanWeiCheckbox)) {
			checkBoxs.add(jianChaDanWeiCheckbox);
		}

		return jianChaDanWeiLayout;
	}

	@Override
	public void notifyDataSetChanged() {
		// 每次notifyDataSetChanged() ;都会加checkbox所以要清空
		checkBoxs.clear();
		super.notifyDataSetChanged();
	}

	public void removeChecked() {
		if (checkBoxs.size() == getCount()) {

			for (int i = 0; i < checkBoxs.size(); i++) {

				if (checkBoxs.get(i).isChecked()) {
					checkBoxs.remove(i);
					datas.remove(i);
					// 删除一项后会将元素前移,所以这里要--
					i--;
				}

			}

		}
	}

}
